<?php

namespace App\Lib;

use App\Http\Model\Hint;
use Illuminate\Http\Request;

class Response {

    public static function json($code, $data = array(), $msg='') {
        $res['code'] = $code;
        $res['msg'] = empty($msg)?(Hint::$Msg[$code] ?? '失败'):$msg;
        $res['data'] = $data;
        return json_encode($res);
    }

    /**
     * @desc 接口数据返回统一方法
     * */
    public static function returnResult($code, $data = array(), $msg='', $aes=1)
    {
        // if ($code == -1){
            // header('HTTP/1.1 500 Internal Server Error');
        // }
        if(empty($msg)) {
            $msg = isset(Hint::$Msg[$code]) ? Hint::$Msg[$code] : '失败';
        }

        $res['code'] = $code;
        $res['msg'] = $msg;
        $res['data'] = $data;

        $jsonStr = json_encode($res,JSON_UNESCAPED_UNICODE);

        //加密
        if (isset($_SERVER['HTTP_NOAES']) && $_SERVER['HTTP_NOAES']==1) {
            $aes = 0;
        }
        if($aes == 1) {
            $jsonStr = enAES($jsonStr);
        }
        echo $jsonStr;
        exit;
    }



}